package com.vogtec.fota.quartz;

import com.vogtec.fota.mqtt.ApplicationContextProvider;
import com.vogtec.fota.service.UpgradeTaskService;
import com.vogtec.fota.service.impl.UpgradeTaskServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Created by wei on 2017/7/24.
 */
@Slf4j
@Component
public class StartJob implements Job {

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {

        log.info("***** in quartz.StartJob *****");

        Integer id = Integer.valueOf(jobExecutionContext.getJobDetail().getJobDataMap().getString("id"));
        try{
            log.info(" the id to start : [{}] ",id);
            UpgradeTaskServiceImpl upgradeTaskServiceImpl = ApplicationContextProvider.getBean(
                    "upgradeTaskServiceImpl", UpgradeTaskServiceImpl.class);
            upgradeTaskServiceImpl.startTask(id);
        }catch (MqttException e){
            e.printStackTrace();
        }
    }
}
